package com.get;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.add.Job1;
import com.add.Job2;
import com.add.Post;

public class GetServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		response.setHeader("content-type","text/html;charset=GBK");
		
		JSONArray members=new JSONArray();
		JSONArray PostMembers=new JSONArray();
		JSONArray JobMembers=new JSONArray();
		JSONObject postmember=new JSONObject();
		JSONObject jobmember=new JSONObject();
		PostImpl pl=new PostImpl();
		Job1Impl jl1=new Job1Impl();
		Job2Impl jl2=new Job2Impl();
		ArrayList postList=pl.getBiList();
		for(int i=0;i<postList.size();i++){
			JSONObject PostMember=new JSONObject();
			Post post=(Post) postList.get(i);
			PostMember.put("postid", post.getId());
			PostMember.put("authorid", post.getAuthorId());
			PostMember.put("joboffer", post.getPlace());
			PostMember.put("date",post.getDate());
			PostMember.put("jobcount", post.getJobCount());
			PostMembers.add(PostMember);
		}
		postmember.put("posts", PostMembers);
		for(int i=0;i<postList.size();i++){
			Post post=(Post) postList.get(i);
			ArrayList<Job1> job1List=jl1.getBiList(post.getId());
			ArrayList<Job2> job2List=jl2.getBiList(post.getId());
			System.out.println(job2List.size());
			if(job1List.size()>0){
				for(int i1=0;i1<job1List.size();i1++){
					JSONObject Job1Member=new JSONObject();
					Job1 j1=job1List.get(i1);
					Job1Member.put("jobid", j1.getId());
					Job1Member.put("postid", j1.getPostId());
					Job1Member.put("jobtype", j1.getJobType());
					Job1Member.put("jobname", j1.getJobName());
					Job1Member.put("paytype", j1.getPayType());
					Job1Member.put("date", j1.getWorkTime());
					Job1Member.put("place", j1.getPlace());
					Job1Member.put("salary", j1.getSalary());
					Job1Member.put("notice", j1.getNotice());
					Job1Member.put("jobstate", j1.getState());
					Job1Member.put("state", j1.getState());
					Job1Member.put("postdate", post.getDate());
					Job1Member.put("joboffer", post.getPlace());
					JobMembers.add(Job1Member);
				}
			}
			if(job2List.size()>0){
				for(int i1=0;i1<job2List.size();i1++){
					JSONObject Job2Member=new JSONObject();
					Job2 j2=job2List.get(i1);
					Job2Member.put("jobid", j2.getId());
					Job2Member.put("postid", j2.getPostId());
					Job2Member.put("jobtype", j2.getJobType());
					Job2Member.put("jobname", j2.getJobName());
					Job2Member.put("paytype", j2.getPayType());
					Job2Member.put("date", j2.getDate());
					Job2Member.put("time1", j2.getTime1());
					Job2Member.put("time2",j2.getTime2());
					Job2Member.put("time3", j2.getTime3());
					Job2Member.put("place", j2.getPlace());
					Job2Member.put("salary", j2.getSalary());
					Job2Member.put("notice", j2.getNotice());
					Job2Member.put("jobstate", j2.getState());
					Job2Member.put("postdate", post.getDate());
					Job2Member.put("joboffer", post.getPlace());
					JobMembers.add(Job2Member);
				}
			}
		}
			jobmember.put("job", JobMembers);
			members.add(postmember);
			members.add(jobmember);
			response.getWriter().print(members);  

			
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		response.setHeader("content-type","text/html;charset=GBK");
		doGet(request, response);
	}

}
